//
//  THGoodCommentViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/17.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THGoodCommentViewController.h"
#import "THGoodCommentTableViewCell.h"
@interface THGoodCommentViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */

@property(nonatomic, strong) NSString                     *allCout;/**<  */
@property(nonatomic, assign) NSInteger                     selected_index;/**<  */
@end

@implementation THGoodCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品评价";
    __weak typeof(self) weakSelf =  self;
    self.tableView.st_noreslutView = [[STTools_NoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 30, STONEOBS_UI_UISCREEN_WIDTH, 400) title:@"暂无数据" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf stoneobs_fun_st_send_list_request];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
- (void)config_table_header{
    if (self.tableView.tableHeaderView.height > 10) {
        return;
    }
    __weak typeof(self) weakSelf =  self;
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    header.backgroundColor = UIColor.whiteColor;
    NSString * title = [NSString stringWithFormat:@"全部(%@)",self.allCout];
    NSArray * names = @[title,@"有图"];
    STTools_TagMenuView * menuview = [[STTools_TagMenuView alloc] initWithFrame:CGRectMake(15, 15, STONEOBS_UI_UISCREEN_WIDTH - 30, 10)];
    menuview.allArray = names;
    menuview.chosedArray = @[title];
    
    menuview.buttonBoderColor = UIColor.clearColor;
    menuview.buttonSelectedBoderColor = WH_UICOLOR_FROM_RGBA(0x37CF34);
    
    menuview.buttonBackGroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
    menuview.buttonSelctedBackGroundColor = WH_UICOLOR_FROM_RGBA(0xD9FED8);
    
    menuview.butTitleColor = WH_UICOLOR_FROM_RGBA(0x666666);
    menuview.butTitleSelectedColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    [header addSubview:menuview];

    [menuview setOnSlectedTagView:^(STTools_TagMenuView *tagView, NSString *title, NSInteger index) {
        weakSelf.selected_index = index;
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    
    header.height = menuview.bottom;
    self.tableView.tableHeaderView = header;
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    THCommentModel * model = self.dataSouce[indexPath.row];
    if (model.cus_height < 10) {
        [self tableView:tableView cellForRowAtIndexPath:indexPath];
    }
    return model.cus_height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    THGoodCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[THGoodCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    THCommentModel * model = self.dataSouce[indexPath.row];
    cell.itemView.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    THCommentModel * model = self.dataSouce[indexPath.row];
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)config_table_footer{
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf stoneobs_fun_st_send_list_request];
    }];
    
}
#pragma mark --stoneobs_net netWork Method
- (void)stoneobs_fun_st_send_list_request{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:@(self.page) forKey:@"page"];
    if (self.detailModel.g_id.length) {
        [stoneobs_net_param_dic setObject:self.detailModel.g_id forKey:@"id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/evaluates"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * dic = [responseObject valueForKey:@"data"];
        NSArray * array = dic[@"items"];
        NSArray * objArray = [THCommentModel mj_objectArrayWithKeyValuesArray:array];
        NSInteger total = [dic[@"total"] integerValue];//总数量
        self.allCout = @(total).description;
        CGFloat perPage = [dic[@"perPage"] floatValue];
        CGFloat allpage = total / perPage; ;
        if (self.page == 1) {
            self.dataSouce = objArray.mutableCopy;
            [self.tableView reloadData];
            if (allpage>1) {
                [self config_table_footer];
            }
        }else{
            if (self.page < allpage) {
                [self.tableView.mj_footer endRefreshing];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.dataSouce addObjectsFromArray:objArray];
            [self.tableView reloadData];
        }
        [self config_table_footer];
        [self.tableView.mj_header endRefreshing];
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}

@end
